/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package comparator;

/**
 *
 * @author martin
 */
public class MovieNameFilter {

    private class Word {

        public String word;
        public boolean remove;

        public Word(String word) {
            this.word = removeQoute(word);
            remove = false;
        }

        private String removeQoute(String word) {
            return word.replace("&quot;", "").replace("&", "");
        }
    }
    private StringBuilder result;
    private Word[] words;
    // private static final String romanNumbersRegex = "^(?=.)(?i)M*(D?C{0,3}|C[DM])(L?X{0,3}|X[LC])(V?I{0,3}|I[VX])(\\.{0,1})$";

    public MovieNameFilter(String movie) {
        this.result = new StringBuilder(movie.length());

        String splitted[] = movie.split(":")[0].split("\\s+");
        words = new Word[splitted.length];
        for (int i = 0; i < splitted.length; i++) {
            words[i] = new Word(splitted[i]);
        }
    }

    private void findMatches(String regex) {
        for (int i = 0; i < words.length; i++) {
            if (!words[i].remove) {
                words[i].remove = words[i].word.matches(regex);
            }

        }
    }

    private void findNumbers() {
        String romanNumbersRegex = "^(?=.)(?i)M*(D?C{0,3}|C[DM])(L?X{0,3}|X[LC])(V?I{0,3}|I[VX])(\\.{0,1})$";
        String arabNumbers = "^([1-9]|[1-9][0-9]|[1-9][0-9][0-9])$";
        String fraction = "(\\d+/\\d+)";
        String numbersAndFractions = "(\\d*)(,?)(\\d+/\\d+)";
        String numbersAndComma = "(\\d+)(,?)(\\d+)";
        String numbersAndComma2 = "(\\d+)(,?)";
        String numbersAndDash = "(\\d*)(-?)(\\d+/\\d+)";
        String numbersAndDash2 = "(\\d*)(-)(\\d+)";
        String qoute = "&quot;";
        findMatches(romanNumbersRegex);
        findMatches(arabNumbers);
        findMatches(fraction);
        findMatches(numbersAndFractions);
        findMatches(numbersAndComma);
        findMatches(numbersAndComma2);
        findMatches(numbersAndDash);
        findMatches(numbersAndDash2);
        findMatches(qoute);
        findMatches("&");
        findMatches("\\bAD -\\b");
        findMatches("\\b- AD\\b");
        findMatches("\\(HD\\)");
        findMatches("\\(18\\+\\)");
        findMatches("\\bMONDAY\\b");
        findMatches("\\bTUESDAY\\b");
        findMatches("\\bWEDNESDAY\\b");
        findMatches("\\bTHURSDAY\\b");
        findMatches("\\bFRIDAY\\b");
        findMatches("\\bSATURDAY\\b");
        findMatches("\\bSUNDAY\\b");
        findMatches("\\bPREMIÉRA\\b");
        findMatches("\\bFILM\\b");
    }

    private void removeBullshit() {
        boolean first = true;
        for (int i = 0; i < words.length; i++) {
            if (!words[i].remove) {
                if (!first) {
                    result.append(" ");
                } else {
                    first = false;
                }
                result.append(words[i].word);
            }
        }
    }

    private void printStatus() {
        for (int i = 0; i < words.length; i++) {
            if (words[i].remove) {
                System.out.println("remove:" + words[i].word);
            }

        }
    }

    public String getName() {
        findNumbers();
        removeBullshit();
        return result.toString();
    }

    public static String processMovieName(String name) {
        MovieNameFilter filter = new MovieNameFilter(name);
        
        String reducedName = filter.getName();
        
        filter = null;
        
        return reducedName;
    }
}
